<template>
  <div class="scroll-wrapper" ref="wrapper">
    <div class="scroll-content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  
  import BScroll from "better-scroll"
  
  export default {
    name: "MyScroll",
    components: {
      // BScroll,
    },
    data() {
      return {
        scroll: null,
      }
    },
    mounted() {
      this.scroll = new BScroll(this.$refs.wrapper, {
        //开启点击事件，使得非button元素可以发生点击事件
        click: true,
        //开启监听滚动，获取滚动坐标
        probeType: 3,
        //开启监听底部事件
        pullUpLoad: true,
      });
      //监听滚动的位置
      this.scroll.on("scroll", position => {
        this.$emit("getScrollPosition", position)
      });
      //监听是否滚动到底部
      this.scroll.on("pullingUp", () => {
        // console.log("bottom");
        this.$emit("pullingUp");
      })
      // console.log("my scroll");
    },
    methods: {
      //滚动到指定位置
      scrollTo(x, y, time = 300) {
        this.scroll.scrollTo(x, y, time)
      },
      //上拉加载完成
      finishPullUp() {
        this.scroll.finishPullUp();
      },
      //更新滚动区域的高度
      refresh() {
        this.scroll.refresh();
        console.log("scroll");
      }
    }
  }
</script>

<style scoped>

</style>
